<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController
{
    public function index()
    {
        $user = M('User');
        $count = $user->where('type=0')->count();
        $page = new \Think\Page($count, 5);
        $page->setConfig('header', '共 %TOTAL_ROW% 条记录');
        $page->setConfig('first', '首页');
        $page->setConfig('last', '末页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('theme', "共 %TOTAL_ROW% 条记录 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $this->assign('show', $page->show());
        // 2.查询数据
        $search = I('get.username');
        if ($search) {
            $data = $user->page(I('get.p', 1), 5)->where('id="' . $search . '" OR username="' . $search . '" OR email="' . $search . '"')->select();
        } else {
            $data = $user->page(I('get.p', 1), 5)->where('type=0')->select();
        }
        // 3.分配数据
        $this->assign('data', $data);
        // 4.输出模板
        $this->display();
    }
    public function mod(){
        //接受用户ID
        $id=I('get.id');
        $user=M('User');
        $info=$user->where('id="'.$id.'" OR username="'.$id.'" OR email="'.$id.'"')->find();
        $this->assign('user',$info);
        $this->display();
    }
    //修改用户数据操作
    public function update(){
        $user=M('user');
        $user->create();
        //进行修改操作
        $result=$user->save();
        if($result){
//            echo "修改成功";
            $JSONObject['code'] =0;
//            //$this->success('修改用户信息成功',U('Admin/index','id='.I('post.id')));
            exit(json_encode($JSONObject));
        }else{
            $JSONObject['code'] =1;
//            //$this->success('修改用户信息成功',U('Admin/index','id='.I('post.id')));
            exit(json_encode($JSONObject));
        }

    }
}